package com.swufe.scoresforapp.Yonghu;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.swufe.scoresforapp.R;

public class YonghuActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.swufe.scoresforapp.Yonghu.MESSAGE";//定义EXTRA_MESSAGE 页面跳转时用
    public static YonghuActivity yonghuzhongxin = null; //方便其他页面操作此页面

    public static messege info = new messege("01","","","","0","0");//存储当前用户信息

    Handler handle;//消息传递句柄

    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences preferences = getSharedPreferences("Data", Context.MODE_PRIVATE);
        info.name = preferences.getString("Name","用户名");
        getYonghu(info.name);
        handle=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 5){
                    info = (messege)msg.obj;
                    setAll(info);
                }
                super.handleMessage(msg);

            }
        };;//消息传递句柄
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yonghu);

        yonghuzhongxin = this;
        //以下为需要添加事件的控件

        //返回按钮
        ImageButton fanhui = (ImageButton)findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用Eventfanhui（）函数即可
                Eventfanhui();
            }
        });

        //更改个人信息按钮
        Button changeinfo = (Button)findViewById(R.id.changeinfo);
        changeinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用Eventchangeinfo()函数即可
                Eventchangeinfo();
            }
        });

    }
    //以下为事件处理函数
    public void Eventfanhui(){
        /*
         点击返回按钮触发事件 返回进入个人中心前的页面
         */
        finish();
    }//点击返回按钮触发事件 返回进入个人中心前的页面

    public void Eventchangeinfo(){
        /*
        点击更改个人信息按钮触发事件
         */
        Intent intent = new Intent(this, ChangeinfoActivity.class);
        intent.putExtra(EXTRA_MESSAGE,"");
        startActivity(intent);
    }//点击更改个人信息按钮触发事件


    //以下为界面一些动态信息的设置
    public void setAll(messege m){
        /*
        设置所有信息，通过结构体messege传递
         */
        //用户名
        TextView name = (TextView)findViewById(R.id.textView);
        name.setText(m.name);
        //分数
        TextView score = (TextView)findViewById(R.id.score);
        score.setText(m.score);
        //排名
        TextView order = (TextView)findViewById(R.id.order);
        order.setText(m.order);
        //地区
        TextView area = (TextView)findViewById(R.id.area);
        area.setText(m.area);
        //科类
        TextView type = (TextView)findViewById(R.id.Type);
        type.setText(m.type);
        //性别
        TextView sex = (TextView)findViewById(R.id.sex);
        sex.setText(m.sex);
        YonghuActivity.info = m;//更改储存信息
    }// 设置所有信息，通过结构体messege传递
    public void setName(String s){
        TextView name = (TextView)findViewById(R.id.textView);
        name.setText(s);
        info.name = s;
    } //设置用户名
    public void setScore(String s){
        TextView score = (TextView)findViewById(R.id.score);
        score.setText(s);
        info.score = s;
    }//设置分数
    public void setOrder(String s){
        TextView order = (TextView)findViewById(R.id.order);
        order.setText(s);
        info.order = s;
    }//设置排名
    public void setArea(String s){
        TextView area = (TextView)findViewById(R.id.area);
        area.setText(s);
        info.area = s;
    }//设置地区
    public void setType(String s){
        TextView type = (TextView)findViewById(R.id.Type);
        type.setText(s);
        info.type = s;
    }//设置科类
    public void setSex(String s){
        TextView sex = (TextView)findViewById(R.id.sex);
        sex.setText(s);
        info.sex = s;
    }//设置性别

    //重新加载页面
    public void refresh(){
        //关闭当前页面并重新加载当前页面
        finish();
        Intent intent = new Intent(this, YonghuActivity.class);
        intent.putExtra(EXTRA_MESSAGE,"");
        startActivity(intent);
    }
    //以下为获取当前页面信息的操作


    //从数据库获取用户信息，接口
    public void getYonghu(final String name){
        new Thread(new Runnable() { //开启线程

            public void run() {
                Dataimport in = new Dataimport();
                String area = in.getArea(name);
                String sex = in.getSex(name);
                String type = in.getType(name);
                String score = in.getScore(name);
                String order = in.getOrder(name);
                messege m = new messege(name,sex,area,type,score,order);
                Message msg = handle.obtainMessage(5);
                msg.obj = m;//发送当前获取的用户信息
                handle.sendMessage(msg);
            }
        }).start();
    }
}
